* Figure 13: Year-by-year individual IV estimates of insurance policy coverage on household outcomes
* Sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if ex_post_balp==1

* Leave treatment expansion in here because this measures individual IV

* Easier labelling for graphs
la var w_real_sav "savings (INR)"
la var w_real_lent "money lent out (INR)"
la var w_real_borrow "money borrowed (INR)"
la var w_real_cons "total consumption (INR)"
la var w_real_food_exp "food expenditure (INR)"
la var w_real_nonfood_exp "non-food expenditure (INR)"
la var w_real_durables_exp "durables expenditure (INR)"
la var w_real_events_exp "events expenditure (INR)"
la var w_real_income "annual income(INR)"
la var w_real_gifts_out "gifts/transfers made (INR)"
la var w_real_gifts_in "gifts/transfers received (INR)"
la var food_sufficiency_child "food sufficiency for children"
la var fladder_std "financial situation (sd units)"
la var outlook_std "good things happen (sd units)"
la var control_std "control over life (sd units)"
sort id mkt_year

local uniqinst "bdmperc_20* fourbdmperc_20* disc4game_20* assigned_risk_ws_2008 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan discount_20* rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv groupT muslimT hinduT sewaT peerT ppayT vframeT pframeT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013" /*mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

sort id mkt_year

// iv
local outcomevars w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std 

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
gen `ovar'effect = .
gen `ovar'se = .
gen `ovar'se2 = .
gen `ovar'se_2 = .
forvalues i = 2006/2013 {
ivreg `ovar' (s_policy_units=treat_year `uniqinst') firstt_2007 firstt_2009 lost_w3 resurveyed if mkt_year == `i' , r cluster(villageno)
replace `ovar'effect = _b[s_policy_units] if mkt_year == `i'
replace `ovar'se = _se[s_policy_units] if mkt_year == `i'
lab var `ovar'effect "No. of Policies Purhchased - Effect"
lab var `ovar'se "Standard Errors"
replace `ovar'se2  = `ovar'effect+2*`ovar'se if mkt_year == `i'
replace `ovar'se_2 = `ovar'effect-2*`ovar'se if mkt_year == `i'
}
}

* did not collect data for these variables in these years
replace w_real_saveffect=. if mkt_year==2007
replace w_real_savse2=. if mkt_year==2007
replace w_real_savse_2=. if mkt_year==2007
replace w_real_lenteffect=. if mkt_year==2008|mkt_year==2007
replace w_real_lentse2=. if mkt_year==2007|mkt_year==2008
replace w_real_lentse_2=. if mkt_year==2007|mkt_year==2008
replace w_real_borroweffect=. if mkt_year==2008|mkt_year==2007
replace w_real_borrowse2=. if mkt_year==2007|mkt_year==2008
replace w_real_borrowse_2=. if mkt_year==2007|mkt_year==2008
replace w_real_gifts_outeffect=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_outse2=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_outse_2=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_ineffect=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_inse2=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_inse_2=. if mkt_year==2008|mkt_year==2007
replace outlook_stdeffect=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace outlook_stdse2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace outlook_stdse_2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace control_stdeffect=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace control_stdse2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace control_stdse_2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009


foreach ovar of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_cons w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
local l`ovar': variable label `ovar'
twoway (rarea `ovar'se2 `ovar'se_2 mkt_year, sort astyle(ci ci))(scatter `ovar'effect mkt_year , sort msymbol(O) c(l)), title("`l`ovar''", size(s)) ylabel(,angle (30)) xlabel(2005(2)2013) xtitle("Year") graphregion(color(white)) plotregion(icolor(white)) legend(order (2 1) label(2 "Estimated coefficient") label(1 "95% confidence interval"))
graph save iv`ovar', replace 
}

* combining graphs
grc1leg ivw_real_sav.gph ivw_real_lent.gph ivw_real_borrow.gph ivw_real_cons.gph ivw_real_food_exp.gph ivw_real_nonfood_exp.gph ivw_real_durables_exp.gph ivw_real_events_exp.gph ivw_real_gifts_out.gph ivw_real_income.gph ivw_real_gifts_in.gph ivfood_sufficiency_child.gph ivfladder_std.gph ivoutlook_std.gph ivcontrol_std.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../output/graphs/f13.emf", replace


